/*
 * Copyright (c) 2010-2011, okazoh_tk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "alkes/media/ImageFileWriter.h"
#include "alkes/core/Memory.h"

namespace alkes {

ImageFileWriter::ImageFileWriter(const char* filepath)
: filepath_(filepath)
, stream_(0)
, pixel_buffer_(0)
{
}

ImageFileWriter::~ImageFileWriter()
{
    if (stream_)
    {
        delete stream_;
    }

    if (pixel_buffer_)
    {
        alkes_free(pixel_buffer_);
    }
}

struct Pixel
{
    uint8_t b, g, r, a;
};

void ImageFileWriter::writeScanline(const PixelFormatTraits* traits, const void* pixels, int32_t /*length*/, int32_t width, int32_t y)
{
    const uint8_t* p = (const uint8_t*)pixels;
    Pixel* dst = ((height_ - y -1) * width_) + (Pixel*)pixel_buffer_;
    int32_t i;
    for (i = 0; i < width; ++i, p += traits->bytes, ++dst)
    {
        Color c(traits->getPixel(p));
        dst->r = c.r;
        dst->g = c.g;
        dst->b = c.b;
        dst->a = c.a;
    }
}

bool ImageFileWriter::onBegin(const PixelFormatTraits* traits, int32_t pitch, int32_t width, int32_t height)
{
    if (4 != traits->bytes)
    {
        return false;
    }

    buffer_size_ = (pitch < 0 ? -pitch : pitch) * height;
    width_ = width;
    height_ = height;

    pixel_buffer_ = (uint8_t*)alkes_malloc(buffer_size_);
    if (!pixel_buffer_)
    {
        return false;
    }

    FileStream* stream = new FileStream(filepath_, AL_ACCESS_WRITE, AL_OPEN_TRUNCATE);
    if (NULL == stream)
    {
        alkes_free(pixel_buffer_);
        pixel_buffer_ = 0;
        return false;
    }

    if (!stream->canWrite())
    {
        alkes_free(pixel_buffer_);
        pixel_buffer_ = 0;
        delete stream;
        return false;
    }

    TgaHeader header;

    header.id = 0;
    header.color_map_type = 0; // no color map
    header.data_type_code = 2; // Unmapped RGB
    header.color_map_origin = 0; // Ignore color map type
    header.color_map_length = 0;
    header.color_map_depth = 0;
    header.xorigin = 0;
    header.yorigin = 0;
    header.width = (int16_t)width;
    header.height = (int16_t)height;
    header.bits_per_pixel = 32;
    header.image_descriptor = 0x00;

    // Write header
    stream->write(sizeof(header), &header);

    stream_ = stream;

    return true;
}

void ImageFileWriter::onEnd()
{
    if (stream_)
    {
        stream_->write(buffer_size_, pixel_buffer_);
        stream_->flush();
        stream_->close();
        delete stream_;
        stream_ = 0;
        alkes_free(pixel_buffer_);
        pixel_buffer_ = 0;
    }
}

}
